package com.project.weimi.activity;

import java.util.ArrayList;
import java.util.List;

import com.project.weimi.R;
import com.project.weimi.adapter.CollectionViewPagerAdapter;
import com.project.weimi.fragment.GoodsFragment;
import com.project.weimi.fragment.InvitationFragment;
import com.project.weimi.utils.SetClickListener;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class MyCollectionActivity extends BaseActivity {

	private ImageView img_left_bar,img_indicator;
	private LinearLayout ll;
	private TextView tv_goods, tv_invitation;
	private TextView tvs[] = new TextView[2];

	private ViewPager vp;
	private List<Fragment> list;

	private GoodsFragment goodsFrag;
	private InvitationFragment invitationFrag;

	// 线性布局宽
	private int linearLayoutWidth;
	// 移动下标宽
	private int indicatorWidth;
	// 当前页面
	private int currentIndex;
	
	public GoodsFragment getGoodsFrag() {
		return goodsFrag;
	}

	public InvitationFragment getInvitationFrag() {
		return invitationFrag;
	}


	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_mycollection);

		initView();
		initIndicator();
		
		initViewPager();

	}

	private void initView() {
		img_left_bar = (ImageView) findViewById(R.id.img_left_bar);
		ll = (LinearLayout)findViewById(R.id.ll);
		tv_goods = (TextView) findViewById(R.id.tv_goods);
		tv_invitation = (TextView) findViewById(R.id.tv_invitation);
		img_indicator = (ImageView)findViewById(R.id.img_indicator);
		vp = (ViewPager) findViewById(R.id.vp);
		
		linearLayoutWidth = ll.getLayoutParams().width;
		//Log.i("aaa", "线性宽======"+linearLayoutWidth);
		tvs[0] = tv_goods;
		tvs[1] = tv_invitation;
		tv_goods.setTextColor(Color.parseColor("#7210B4"));

		// 监听
		SetClickListener.setClickListener(listener, img_left_bar, tv_goods, tv_invitation);

	}

	private void initIndicator(){
//		LayoutParams p = (LayoutParams) img_indicator.getLayoutParams();
//		int leftMargin = p.leftMargin;
//		indicatorWidth = (int) (linearLayoutWidth / 2.0 - leftMargin * 2);
//		
//		Log.i("aaa", "线性宽======"+linearLayoutWidth+";移动下标宽----"+indicatorWidth);
//		p.width = indicatorWidth;
//		img_indicator.setLayoutParams(p);
//		
//		Animation animation = new TranslateAnimation(0, leftMargin, 0, 0);
//		animation.setFillAfter(true);
//		animation.setDuration(300);
//		img_indicator.startAnimation(animation);
		
		indicatorWidth = linearLayoutWidth / 2;
		LayoutParams params = (LayoutParams) img_indicator.getLayoutParams();
		params.width = indicatorWidth;
		img_indicator.setLayoutParams(params);
		
	}
	
	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.img_left_bar:
				finish();
				break;
			case R.id.tv_goods:
				vp.setCurrentItem(0);
				break;
			case R.id.tv_invitation:
				vp.setCurrentItem(1);
				break;
			default:
				break;
			}

		}
	};

	private void initViewPager() {
		list = new ArrayList<Fragment>();
		goodsFrag = new GoodsFragment();
		invitationFrag = new InvitationFragment();
		list.add(goodsFrag);
		list.add(invitationFrag);
		FragmentManager manager = getSupportFragmentManager();

		CollectionViewPagerAdapter adapter = new CollectionViewPagerAdapter(manager, list);
		vp.setAdapter(adapter);
		
		// ViewPager滑动监听
		vp.addOnPageChangeListener(pageChangeListener);

	}

	private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {
			textViewColor(arg0);
			currentIndex = arg0;
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			//从左到右
			if(currentIndex == arg0) {
				LayoutParams layoutParam = (LayoutParams) img_indicator.getLayoutParams();
				layoutParam.leftMargin = (int) (arg1 * (linearLayoutWidth * 1.0 / 2) + currentIndex * (linearLayoutWidth / 2));
				img_indicator.setLayoutParams(layoutParam);
				
			}
			//从右到左
			else if(currentIndex > arg0){
				LayoutParams layoutParam = (LayoutParams) img_indicator.getLayoutParams();
				layoutParam.leftMargin = (int) (-(1-arg1) * (linearLayoutWidth * 1.0 / 2) + currentIndex * (linearLayoutWidth / 2));
				img_indicator.setLayoutParams(layoutParam);
			}
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}
	};

	private void textViewColor(int index) {
		for (int i = 0; i < tvs.length; i++) {
			if (i == index) {
				tvs[i].setTextColor(Color.parseColor("#7210B4"));
			} else {
				tvs[i].setTextColor(Color.parseColor("#2D2D2D"));
			}
		}
	}

	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}

}
